<?php
namespace controller;

use app\store\service\MemberService;
use service\ToolsService;
use service\WechatService;
use think\Controller;
use think\Db;

/**
 * 微信基础控制器
 * Class BasicWechat
 * @package controller
 */
class BasicWechat extends Controller
{

    /**
     * 当前粉丝用户OPENID
     * @var string
     */
    protected $openid;

    /**
     * 当前会员数据记录
     * @var array
     */
    protected $member = [];

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $userinfo = $this->getFansinfo();
        if (!empty($userinfo['openid'])){
            $this->openid = $userinfo['openid'];
            $user = Db::table('user')->where('openid',$userinfo['openid'])->find();
            if ($user){
                $user['nickname'] = ToolsService::emojiDecode($user['nickname']);
                $this->member = $user;
                if ($user['is_deleted']==2){
                    $this->redirect('index/notfound');
                }
            }else{
                $data = [
                    'nickname'=>ToolsService::emojiEncode($userinfo['nickname']),
//                    'sex'=>$userinfo['sex']==1 ? '男' : ($userinfo['sex']==2 ? '女' : '未知'),
                    'sex'=>$userinfo['sex'],
                    'headimg'=>$userinfo['headimgurl'],
                    'area'=>$userinfo['province'].$userinfo['city'],
                    'openid'=>$userinfo['openid'],
                    'create_at'=>date('Y-m-d H:i:s')
                ];
                Db::table('user')->insertGetId($data);
                $this->member = Db::table('user')->where('openid',$userinfo['openid'])->find();
            }

        }
    }

    /**
     * 手机号拦截
     */
    public function verifyphone(){
        if (empty($this->member['phone'])){
            $url = $this->request->url(true);
            session('oldurl',$url);
            $this->redirect('/index/login/login');
        }
    }

    /**
     * 必须绑定门店
     */
    public function verifymid(){
        if (empty($this->member['mid'])){
//            $url = $this->request->url(true);
//            session('oldurl',$url);
            $this->redirect('/index/entry/index');
        }
    }



    /**
     * 初始化会员数据记录
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @return array
     */
//    protected function initMember()
//    {
//        $openid = $this->getOpenid();
//        $this->member = Db::name('StoreMember')->where(['openid' => $openid])->find();
//        if (empty($this->member)) {
//            MemberService::create(['openid' => $openid]);
//            $this->member = Db::name('StoreMember')->where(['openid' => $openid])->find();
//        }
//        return $this->member;
//    }

    /**
     * 获取粉丝用户OPENID
     * @return bool|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    protected function getOpenid()
    {
        $url = $this->request->url(true);
        return WechatService::webOauth($url, 0)['openid'];
    }

    /**
     * 获取微信粉丝信息
     * @return bool|array
     * @throws \Exception
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    protected function getFansinfo()
    {
        $url = $this->request->url(true);
        return WechatService::webOauth($url, 1)['fansinfo'];
    }

}
